﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ClipboardEx
{
    public partial class ConfigFormcs : Form
    {
        public ConfigFormcs()
        {
            InitializeComponent();

            numericUpDown1.Value = Form1.SConfig.MaxRecord;
            checkBox1.Checked = Form1.SConfig.StartWithWin;
            checkBox2.Checked = Form1.SConfig.CloseForMin;
            checkBox4.Checked = Form1.SConfig.PopWhenClipChange;
            checkBox5.Checked = Form1.SConfig.UseHotKey;

            radioButton1.Checked = Form1.SConfig.IsControlChecked(MouseButtons.Left, 1, appconfig.EControlType.Copy);
            radioButton2.Checked = Form1.SConfig.IsControlChecked(MouseButtons.Left, 2, appconfig.EControlType.Copy);
            radioButton3.Checked = Form1.SConfig.IsControlChecked(MouseButtons.Right, 1, appconfig.EControlType.Del);
            radioButton4.Checked = Form1.SConfig.IsControlChecked(MouseButtons.Right, 2, appconfig.EControlType.Del);

            comboBox1.Enabled = checkBox5.Checked;
            comboBox2.Enabled = checkBox5.Checked;
            comboBox1.Text = Form1.SConfig.GetConfig(appconfig.EConfigTxt.CONFIGSTR_K1);
            comboBox2.Text = Form1.SConfig.GetConfig(appconfig.EConfigTxt.CONFIGSTR_K2);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void saveDonfig()
        {
            Form1.SConfig.MaxRecord = (int)numericUpDown1.Value;
            bool bChange = Form1.SConfig.StartWithWin != checkBox1.Checked;
            Form1.SConfig.StartWithWin = checkBox1.Checked;
            Form1.SConfig.CloseForMin = checkBox2.Checked;
            Form1.SConfig.PopWhenClipChange = checkBox4.Checked;
            Form1.SConfig.UseHotKey = checkBox5.Checked;

            if (radioButton1.Checked)
            {
                Form1.SConfig.SetControlChecked(MouseButtons.Left, 1, appconfig.EControlType.Copy);
            }

            if (radioButton2.Checked)
            {
                Form1.SConfig.SetControlChecked(MouseButtons.Left, 2, appconfig.EControlType.Copy);
            }

            if (radioButton3.Checked)
            {
                Form1.SConfig.SetControlChecked(MouseButtons.Right, 1, appconfig.EControlType.Del);
            }

            if (radioButton4.Checked)
            {
                Form1.SConfig.SetControlChecked(MouseButtons.Right, 2, appconfig.EControlType.Del);
            }
            
            bool bHotKeyChange = false;

            bHotKeyChange = Form1.SConfig.SetConfig(appconfig.EConfigTxt.CONFIGSTR_K1, comboBox1.Text);
            bHotKeyChange |= Form1.SConfig.SetConfig(appconfig.EConfigTxt.CONFIGSTR_K2, comboBox2.Text);

            string exepath = Application.StartupPath;
            Form1.SConfig.SaveConfig(exepath + "\\config.xml");

            // 开机启动有变化才执行
            if (bChange)
            {
                registryme(Form1.SConfig.StartWithWin);
            }

            // 热键注册有变化执行
            if (bHotKeyChange)
            {
                ((Form1)Owner).HotkeyRegister();
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            saveDonfig();
            Close();
        }

        private void registryme(bool regist)
        {
            // 将信息写入注册表
            string startPath = Application.ExecutablePath;
            RegistryKey loca = Registry.CurrentUser;
            RegistryKey run = loca.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);

            try
            {
                if (regist)
                {
                    run.SetValue("ClipboardEx", startPath);
                }
                else
                {
                    run.DeleteValue("ClipboardEx");
                }
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show(ex.Message, ex.GetType().FullName);
            }
            finally
            {
                loca.Close();
            }
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            comboBox1.Enabled = cb.Checked;
            comboBox2.Enabled = cb.Checked;
        }
    }
}
